import {selector} from 'recoil';
import {todoListState,filterState} from './atoms';
/**
 * 定义一个派生的状态，名为过滤后的todo列表状态
 */
export const filteredTodoListState = selector({
    key:'filteredTodoListState',//定义状态的名称, key是一个字符串，用来标识此状态
    get:({get})=>{//参数对象中可以解构出get属性，用于获取其它的状态
        const todoList = get(todoListState);
        const filter = get(filterState);
        switch(filter){
            case 'SHOW_COMPLETED':
                return todoList.filter(todo=>todo.completed);
            case 'SHOW_UNCOMPLETED':
                return todoList.filter(todo=>!todo.completed);
            case 'SHOW_ALL':    
            default:
                return todoList;
        }
    }
});